package com.ynet.hmds28.user.impl;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.boot.hsf.annotation.HSFProvider;
import com.ynet.hm.user.service.ServiceRealTimeControlService;
import com.ynet.hm.utils.config.Result;
import com.ynet.hm.utils.config.ResultUtil;
import com.ynet.hmds28.user.dao.PlatflowDao;
import com.ynet.hmds28.user.model.Platflow;
import com.ynet.hmds28.user.model.PlatflowQuery;
import com.ynet.hmds28.user.redis.RedisService;
@Transactional
@HSFProvider(serviceInterface = ServiceRealTimeControlService.class, serviceVersion = "1.0.0",serviceGroup = "hmds28")
public class ServiceRealTimeControlServiceImpl implements
		ServiceRealTimeControlService {
	@Autowired
	private PlatflowDao platflowDao;
	@Autowired
	private RedisService redisService;
	@Override
	public Result<?> serviceRealTimeControl(String serviceProviceId) {
		long stime=(long)redisService.get("projectStartTime");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        Date temp=null;
        try {  
            String str=sdf.format(stime);  
            temp=sdf.parse(str);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } 
		PlatflowQuery pfq=new PlatflowQuery();
		pfq.createCriteria().andSerivceprovideidEqualTo(serviceProviceId);
		List<Platflow> pfs=platflowDao.selectByExample(pfq);
		int totalNum=0;
		int successRate=0;
		String aveMakeTime="0";
		String tps="0";
		if(!CollectionUtils.isEmpty(pfs)){
			totalNum=pfs.size();
			int successNum=0;
			int totalTime=0;
			for(Platflow pf:pfs){
				successNum+=Integer.parseInt(pf.getIssuccess());
				totalTime+=Integer.parseInt(pf.getMaketime());
			}
			//求成功率
			NumberFormat numberFormat = NumberFormat.getInstance();
			// 设置精确到小数点后0位
			numberFormat.setMaximumFractionDigits(0);
			successRate = Integer.parseInt(numberFormat.format((float) successNum / (float) totalNum * 100));
			//求平均耗时
			DecimalFormat df = new DecimalFormat("0.00");//格式化小数  
			aveMakeTime = df.format((float)totalTime/totalNum);
			
		}
		
		PlatflowQuery pfq1=new PlatflowQuery();
		pfq1.createCriteria().andSerivceprovideidEqualTo(serviceProviceId).andCalltimeGreaterThanOrEqualTo(temp);
		List<Platflow> pfs1=platflowDao.selectByExample(pfq1);
		if(!CollectionUtils.isEmpty(pfs1)){
			int successNum=0;
			for(Platflow pf:pfs1){
				successNum+=Integer.parseInt(pf.getIssuccess());
			}
			DecimalFormat df = new DecimalFormat("0.00");//格式化小数  
			//计算tps
			long startTime=Long.parseLong(redisService.get("projectStartTime").toString());
			long now=System.currentTimeMillis();
			int subTime=(int)(now-startTime)/1000;
			tps= df.format((float)successNum/subTime);
		}
		
		Map<String,Object> m=new HashMap<>();
		m.put("totalNum", totalNum);
		m.put("successRate", successRate);
		m.put("aveMakeTime", aveMakeTime);
		m.put("tps", tps);
		return ResultUtil.success(m);
	}

}
